package Thread;


//互斥锁.
//synchronized也叫互斥锁,即:
//使用synchronized修饰多段代码，只要他们的
//同步监视器对象相同，那么这几段代码间就是
//互斥关系，即:多个线程不能同时执行这些代码

public class SynchronizedDemo4 {
    public static void main(String[] args) {
        Boo boo=new Boo();
        Thread thread1=new Thread(){
            @Override
            public void run() {
                boo.method1();
            }
        };

        Thread thread2=new Thread(){
            @Override
            public void run() {
                boo.method2();
            }
        };
        thread1.start();
        thread2.start();
    }
}

class Boo{
    public synchronized void method1(){
        Thread thread=Thread.currentThread();
        System.out.println(thread.getName()+"在执行method1方法");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行method1方法完毕");
    }
    public synchronized void method2(){
        Thread thread=Thread.currentThread();
        System.out.println(thread.getName()+"在执行method2方法");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行method2方法完毕");
    }
}
